<? 
//include ('data.php');
if (array_key_exists('choose_bg_form_submit_check', $_POST)) {  //if anything posted from choose_bg_form.php
$project_id = $_POST['project_id'];
//echo 'project id: '.$project_id.'<br />';
$background_id = $_POST['background'];
//echo 'asset id: '.$background_id.'<br />';
$bg_delete = $_POST['bg_delete'];
//echo 'remove: '.$bg_delete.'<br />';
if ($project_id <> NULL && $background_id <> NULL)  {
	$query = "select * from assets where id = ".$background_id;
$result = mysql_query($query);
if ( !$result )
{
	echo ( "<P>Error doing a select for background: " . mysql_error() . "</P>" );
}
else
{
$nextresult = mysql_fetch_array($result);
$width = $nextresult['width'];
//echo 'width is: '.$width.'<br />';
$height = $nextresult['height'];
//echo 'height is: '.$height.'<br />';
$filename = $nextresult['filename'];
//echo 'filename is: '.$filename.'<br />';

} //else
} // top if
else {
	echo 'No project id';
}
//now store the background info in the projects table
$query = "UPDATE projects SET background = '$filename', width = '$width', height = '$height' WHERE id = '$project_id'";
$result = mysql_query($query);
if ( !$result )
{
	echo ( "<P>Error updating project - save_background.php: " . mysql_error() . "</P>" );
}
else
{
	//echo '<p>project successfully updated</p>';
	//echo '<p><a href="../ed04.php?project_id='.$project_id.'">Click to place markers</a></p>';
}
// ************ Now deal with the delete box if checked ******************
	if ($bg_delete == $background_id) {
	echo 'Cannot delete selected background';
	} elseif ($bg_delete <> NULL) {  //check if any image was to be deleted
		//delete image
		$query = "select * from assets where id = ".$bg_delete;
		$result = mysql_query($query);
		if ( !$result ){
			echo ( "<P>Error doing a select for assets: " . mysql_error() . "</P>" );
		} else {
				$nextresult = mysql_fetch_array($result);
				$filename = $nextresult['filename'];
				$icon = $nextresult['icon'];
				//echo 'filename is: '.$filename.'<br />';
				//echo 'icon is: '.$icon.'<br />';
				//ok to here
				$result = unlink($filename);
					if ($result) { $result = unlink($icon); }
					if ($result) {
					//remove from database
					$query = "DELETE FROM assets where id = ".$bg_delete;
					$result = mysql_query($query);
						if ( !$result )	{
						echo ( "<P>Error deleting background - save_background.php: " . mysql_error() . "</P>" );
						} else {
						//echo '<p>project successfully updated</p>';
						//echo '<p><a href="../ed04.php?project_id='.$project_id.'">Click to place markers</a></p>';
						}
					} //if      
		} //else
	}//else
} // top if
?>